﻿using Common.Contract.Authentication.Record;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand.Admin
{
    public class RetrieveDocumentInfoByDocIdServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveDocumentInfoByDocIdRequest, RetrieveDocumentInfoByDocIdResponse>
    {
        #region Properties
        public UserRecord Dao;
        public string TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public DateTime? FromDate;
        public int DocumentId;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveDocumentInfoByDocIdServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveDocumentInfoByDocIdRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.docId == 0)
            {
                throw new ArgumentException("Product must be set", "request");
            }
            else
            {
                DocumentId = request.docId;
            }

            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
        }

        protected override bool OnExecute(RetrieveDocumentInfoByDocIdResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                ChecklistDocumentRecord record = new ChecklistDocumentRecord();

                CheckListDocument documentDetail = new CheckListDocument();
                documentDetail = Providers.ProductHandbooks.RetrieveChecklistDocumentByDocId(DocumentId);
                if (documentDetail != null)
                {
                    record = new ChecklistDocumentRecord
                    {
                        Document = documentDetail.Document,
                        SubProductId = documentDetail.SubProductId,
                        Copy = documentDetail.Copy,
                        DocumentGroup = documentDetail.DocumentGroup,
                        DocumentGroupCategory = documentDetail.DocumentGroupCategory,
                        Duplicate = documentDetail.Duplicate,
                        PictureDoc = documentDetail.PictureDocument,
                        Id = documentDetail.ChecklistDocId.ToString(),
                        Original = documentDetail.Original,
                        Note = documentDetail.Note
                    };
                }

                response.Record = record;
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
